import "reflect-metadata";
export function Get(path: string = ""): MethodDecorator {
  /**
   * target 类原型 AppController.prototype
   * propertyKey 方法名 index
   * descriptor index 方法的属性描述器
   */
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    //给descriptor.value添加path属性  值为path 也就是给index添加元数据 path=path
    Reflect.defineMetadata("path", path, descriptor.value);
    //给descriptor.value添加path属性  值为path 也就是给index添加元数据 method=GET
    Reflect.defineMetadata("method", "GET", descriptor.value);
  };
}

export function Post(path: string = ""): MethodDecorator {
  /**
   * target 类原型 AppController.prototype
   * propertyKey 方法名 index
   * descriptor index 方法的属性描述器
   */
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    //给descriptor.value添加path属性  值为path 也就是给index添加元数据 path=path
    Reflect.defineMetadata("path", path, descriptor.value);
    //给descriptor.value添加path属性  值为path 也就是给index添加元数据 method=GET
    Reflect.defineMetadata("method", "Post", descriptor.value);
  };
}

export function Redirect(
  url: string = "/",
  statusCode: number = 302
): MethodDecorator {
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    Reflect.defineMetadata("redirectUrl", url, descriptor.value);
    Reflect.defineMetadata("redirectStatusCode", statusCode, descriptor.value);
  };
}

export function HttpCode(statusCode: number = 302): MethodDecorator {
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    Reflect.defineMetadata("statusCode", statusCode, descriptor.value);
  };
}

export function Header(name: string, value: string): MethodDecorator {
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    const existingParameters =
      Reflect.getMetadata(`headers`, descriptor.value) ?? [];
    existingParameters.push({ name, value });
    Reflect.defineMetadata(`headers`, existingParameters, descriptor.value);
  };
}
